/*
 * COMP6471 Project4
 * 
 * Assignment 4: Due 23:59 Sunday 6 November 2011. 
 * Submit a .tar.gz file with your Java program, test input files, and test outputs to Project 4 of 
 * electronic submission system. 
 * A batch Java program to generate games from game layouts; ie it adds the arithmetic formulas. 
 * Your game must have a solution: do this but first filling in the layout, and then selecting a 
 * formula where the cage values match the formula. 
 * Ideally you should only create games that have a unique solution.

 * 
 * This file is created by Yuan Tao (ewan.msn@gmail.com)
 * Licensed under GNU GPL v3
 * 
 * $Author: ewan.msn@gmail.com $
 * $Date: 2011-11-05 20:01:26 +0000 (Sat, 05 Nov 2011) $
 * $Rev: 50 $
 * $HeadURL: http://comp6471.googlecode.com/svn/Project4/src/Project4Main.java $
 * 
 */

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import kklib.*;
import util.*;

public class Project4Main {
	
	public static ArrayList<Layout> layouts = new ArrayList<Layout>();

	// result of project 2.
	public static ArrayList<ArrayList<Integer>> possibleLayouts = new ArrayList<ArrayList<Integer>>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hi, wellcome to COMP6471 Project4. v0.1");
		System.out.println("Developed by Yuan Tao (ewan.msn@gmail.com)\n");
		System.out.println("Please put all the input files under the root directoy of $ThisProgram\\input\\");
		System.out.println("There are already some sample txt files under $input directory.");
		System.out.println("The result of the progrom will be stored in the file: $ThisProgram\\output\\result.txt \n");
		System.out.println("This project is based on project 2. It will first generate all the possible layouts with " +
				"the shapes got from input file and then add all possible formulas to each layout. \n" +
				"There might be a large number of results. Please give the program a few seconds.");
		System.out.println("Then press any key to begin...");
		
		try {
			System.in.read();
		} catch (IOException e) {
			
		}

		// create and initialize the logger
		logger.init();

		// format the result.txt file.
		SimpleDateFormat tmpDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datetimeNow = tmpDate.format(new java.util.Date());
		logger.output(datetimeNow + "\n");
		
		// load all txt files
		InputLoader txtLoader = new InputLoader();
		String path = System.getProperty("user.dir") + "\\input";
		if (txtLoader.loadTextFiles(path, layouts) != 0) {
			return;
		}

		// checking
		Layout layout = null;
		for (int i = 0; i < layouts.size(); i++) {
			layout = layouts.get(i);

			// logging
			logger.info(layout.fileFullname);
			//logger.info(layout.txtFileData.toString());

			// parse one file
			if (txtLoader.fileDataParserForCages(layout) != 0) {
				continue;
			}
			
			for (int j = 0; j < layout.availableCages.size(); j++) {
				logger.info(layout.availableCages.get(j).cageType + ":" +
						layout.availableCages.get(j).cageNum + " ");
			}
			
			// Generate the possible layouts
			possibleLayouts.clear();
			int ret = layout.generatePossibleLayoutFromCages(possibleLayouts);
			
			logger.output("Input filename: " + layout.fileFullname);
			if (ret == -3) {
				logger.output("Number of possible layouts WITHOUT formulas: > " + Layout.MAX_POSSIBLE_LAYOUTS);
			} else {
				logger.output("Number of possible layouts WITHOUT formulas: " + layout.getNumOfSolutions());
			}
			
			if (ret == -1) {
				logger.output("Error: Too many cages.\n");
			} else if (ret == -2) {
				logger.output("Error: Too few cages.\n");
			} else {
				// limit the layout numbers
				if (possibleLayouts.size() > Layout.MAX_LAYOUTSOLUTIONS) {
					logger.output("Only the first " + Layout.MAX_LAYOUTSOLUTIONS + "layouts will be analyzed.");
				}
				
				// for each layout, try to add all the possible formulas
				for (int j = 0; j < possibleLayouts.size(); j++) {
					logger.output("Here are all the possible layouts WITH formulas having only one solution for layout NO. " + (j + 1) + ":");
					layout.generateLayoutWithFormulas(possibleLayouts.get(j));
				}
			}
		}
		
		System.out.println("The program ends successfully!");
		System.out.println("The result of the progrom has been stored in the file: $ThisProgram\\output\\result.txt \n");
	}

}
